import java.sql.SQLException;
import java.util.List;
import daoimpl.MySQLOperatoerDAO;
import daoimpl.MySQLProduktBatchDAO;
import daoimpl.MySQLRaavareBatchDAO;
import daoimpl.MySQLRaavareDAO;
import daoimpl.MySQLReceptDAO;
import dto.ProduktBatchDTO;
import dto.RaavareBatchDTO;
import dto.ReceptDTO;
import daointerfaces.DALException;

public class DatabaseConnecter {

	private MySQLOperatoerDAO operatorDAO;
	private MySQLProduktBatchDAO produktBatchDAO;
	private MySQLRaavareDAO raavareDAO;
	private MySQLReceptDAO receptDAO;
	private MySQLRaavareBatchDAO raavareBatchDAO;

	public DatabaseConnecter (){
		loadDatabase();
	}

	private void loadDatabase(){
		try {
			new connector.Connector();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		operatorDAO = new daoimpl.MySQLOperatoerDAO();
		produktBatchDAO = new daoimpl.MySQLProduktBatchDAO();
		raavareDAO = new daoimpl.MySQLRaavareDAO();
		receptDAO = new daoimpl.MySQLReceptDAO();
		raavareBatchDAO = new daoimpl.MySQLRaavareBatchDAO();
	}

	public String getOperator(int oprId) throws DALException{
		return operatorDAO.getOperatoer(oprId).getOprNavn();
	}

	public String findItem(int productID){

		try {
			return raavareDAO.getRaavare(productID).getRaavareNavn();
		} catch (DALException e) { e.printStackTrace(); }
		throw new NullPointerException();
	}

	public String getProductBatchReceptName(int tempID) throws DALException {
		int tempReceptID = produktBatchDAO.getProduktBatch(tempID).getReceptId();
		return receptDAO.getRecept(tempReceptID).getReceptNavn();
	}

	public List<ProduktBatchDTO> getProductBatchGroup(int tempID) throws DALException {
		return produktBatchDAO.getProduktBatchGroup(tempID);
	}

	public RaavareBatchDTO getRaavareBatchDTO(int raavareId){
		try {
			return raavareBatchDAO.getRaavareBatch(raavareId);
		} catch (DALException e) {
			e.printStackTrace();
		}
		return null;
	}

	public double getReceptRaavareTol(int receptId, int raavareId) throws DALException{
		List<ReceptDTO> tempRaavareList = receptDAO.getReceptListNr(receptId);
		for(ReceptDTO recept: tempRaavareList){
			if(recept.getRaavareId() ==  raavareId){
				return recept.getTolerance();
			}
		}
		throw new DALException("RaavareID: "+raavareId+" not in receptID: "+receptId);
	}

	public ReceptDTO getReceptDTO(int receptId) {
		try {
			return receptDAO.getRecept(receptId);
		} catch (DALException e) {
			e.printStackTrace();
		}
		return null;
	}

	public double getNomNetto(int tempReceptID, int raavareID) throws DALException {
		List<ReceptDTO> tempRaavareList = receptDAO.getReceptListNr(tempReceptID);
		for(ReceptDTO recept: tempRaavareList){
			if(recept.getRaavareId() ==  raavareID){
				return recept.getNomNetto();
			}
		}
		throw new DALException("RaavareID: "+raavareID+" not in receptID: "+tempReceptID);
	}

	public void updateProductBatch(ProduktBatchDTO tempProductBatchRaavare) throws DALException{
		produktBatchDAO.updateProduktBatch(tempProductBatchRaavare);
	}

	public void updateProductBatchTimeStatusNetto(ProduktBatchDTO tempProductBatchRaavare) throws DALException{
		produktBatchDAO.updateProduktBatch_TimeStatusNettoOpr(tempProductBatchRaavare);
	}

	public void updateRaavareBatch(RaavareBatchDTO tempRaavareBatch) throws DALException{
		raavareBatchDAO.updateRaavareBatch(tempRaavareBatch);
	}
}